package com.zh.yqbj.controller;

import cn.dev33.satoken.util.SaResult;
import com.zh.yqbj.service.UFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/file/")
@Slf4j
public class FileController {

    @Autowired
    private UFileService uFileService;

    // 文件上传 （可以多文件上传）
    @PostMapping("/upload")
    public SaResult fileUploads(MultipartFile[] files) throws IOException {
        // 交给FileService处理
        List<String> uploadFiles = uFileService.fileUpload(files);
        return SaResult.data(uploadFiles);
    }

    @GetMapping("/download")
    public ResponseEntity<FileSystemResource> downloadFile(@RequestParam("filePath") String filePath) {
        File file = new File(filePath); // 根据文件路径创建文件对象
        return ResponseEntity.ok()
                .header("Content-Disposition", "attachment; filename=" + file.getName())
                .body(new FileSystemResource(new File(filePath)));
    }
}
